

//   随机颜色
function randomcolor(){
    var str = "#"
    var arr = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
    for(var i = 0 ;i <6; i++){
      
      str = str + arr[Math.floor(Math.random()*(arr.length-1 - 0 +1)+0)]
       
    }
    
    return str
}





//  通用获取样式
function getStyle(ele,attr){
    if(ele.currentStyle){
        return ele.currentStyle[attr]
    }else{
        return getComputedStyle(ele,false)[attr]
    }
}
//第一个参数为需要获取样式的元素，第二个参数为css样式，需要用引号





// 绑定 DOM  2级事件 的 兼容方法
function addEvent(ele,incident,fn,bol){
    if(ele.attachEvent){
        ele.attachEvent("on"+incident,fn)
    }else{
        ele.addEventListener(incident,fn,bol);

    }
}

function $ajax(method,url,async,data,call){
    let xhr = new XMLHttpRequest()
    let arrstr = ''
    if(data!=null){
        console.log(data);
       for(let n in data){
           
            arrstr+=`${n}=${data[n]}&`
       }
       arrstr = arrstr.substring(0,arrstr.length-1)

    }
   


    if(method === 'post'){
        xhr.open(method,url,async)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
    }else if(method === 'get'){
        url = url+'?'+arrstr
        xhr.open(method,url,async)
    }

    

    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res = JSON.parse(xhr.responseText)
                call(res)
            }
        }
    }
   
    if(method === 'get'){
        xhr.send(null)
    }else if(method === 'post'){
        xhr.send()
    }
}   

let obj = {}





// 封装返回顶部函数
function toTop() {
    $('main section .pub1 ul li:eq(2)').click(function () {
        $('main').animate({
            scrollTop:0
        },400)
    })
}

function toTop1() {
    $('main .pub1 ul li:eq(2)').click(function () {
        $('html,body').animate({
            scrollTop:0
        },400)
    })
}
function toTop2() {
    $('section .pub1 ul li:eq(2)').click(function () {
        $('html,body').animate({
            scrollTop:0
        },400)
    })
}
